/*
 * @Author: zhangfeng
 * @Date: 2023-11-30 10:46:33
 * @LastEditTime: 2024-01-30 11:32:07
 * @LastEditors: zhangfeng
 * @Description:
 * @FilePath: \manager\managersystem\vite.config.ts
 * © 2023 张峰，All rights reserved.
 */
// import { defineConfig, loadEnv } from 'vite';
import { defineConfig, loadEnv } from 'vite';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import path from 'path';
import vue from '@vitejs/plugin-vue';
import VueJsx from '@vitejs/plugin-vue-jsx';
const env = loadEnv('mode', process.cwd(), '');
const { VITE_API_BASE, VITE_API_PREFIX, VITE_API_URL } = env;

// https://vitejs.dev/config/
export default defineConfig({
	base: VITE_API_BASE,
	server: {
		host: '0.0.0.0',
		port: 8080,
		strictPort: false,
		open: true,
		proxy: {
			[VITE_API_PREFIX]: {
				target: VITE_API_URL,
				changeOrigin: true,
				rewrite: (path) => path.replace(new RegExp(`^${VITE_API_PREFIX}`), ''),
			},
		},
	},
	build: {
		target: 'es2015',
	},
	plugins: [
		vue(),
		VueJsx(),
		AutoImport({
			imports: ['vue', 'vue-router'],
			resolvers: [
				ElementPlusResolver(),
				// Auto import icon components
				// 自动导入图标组件
				IconsResolver({
					prefix: 'Icon',
				}),
			],
			dts: './src/types/auto-imports.d.ts',
			eslintrc: {
				enabled: true, // Default `false`
			},
		}),
		Components({
			resolvers: [
				// Auto register icon components
				// 自动注册图标组件
				IconsResolver({
					enabledCollections: ['ep'],
				}),
				ElementPlusResolver({
					importStyle: 'sass',
					directives: true,
					version: '2.1.5',
				}),
			],
			dts: './src/types/components.d.ts',
		}),
		// 图标自动下载
		Icons({
			autoInstall: true,
		}),
	],
	esbuild: {
		jsxFactory: 'h',
		jsxFragment: 'Fragment',
	},
	css: {
		preprocessorOptions: {
			scss: {
				additionalData: `@use "./src/styles/theme.scss" as *;
        @use "./src/styles/index.scss";`,
			},
		},
	},
	resolve: {
		alias: {
			'@': path.resolve(__dirname, 'src'),
		},
	},
});
